/*************************************************************************
 *
 * funkce pro obsluhu LCD dipleje vyssi urovne
 * 
 * (c) Ales Povalac, alpov@alpov.net, http://www.alpov.net/
 * 
 *************************************************************************
 *
 * $Id: menu_lcd.c 69 2006-08-03 11:32:05Z alpov $
 * 
 *************************************************************************/         

#include <stdio.h>
#include <avr/io.h>
#include "../avrx/avrx.h"
#include "../avrx/AvrXFifo.h"
#include "menu_lcd.h"
#include "lcd.h"
#include "data.h"

static const PROGMEM unsigned char degCelsius[8] =
    {0x18, 0x1B, 0x04, 0x04, 0x04, 0x04, 0x03, 0x00};

static int lcd_putchar(char c, FILE *stream);
static FILE mystdout = FDEV_SETUP_STREAM(lcd_putchar, NULL, _FDEV_SETUP_WRITE);


/*************************************************************************
 * inicializace znakove sady (stupen celsia), zarizeni lcd
 * pro vystup pomoci printf a PWM pro rizeni jasu
 *************************************************************************/ 
void LcdInitHiLevel(void)
{
    unsigned char i;

    lcd_command(_BV(LCD_CGRAM) + (7 << 3));
    for (i = 0; i < 8; i++) {
        lcd_data(pgm_read_byte_near(&degCelsius[i]));
    }
    lcd_gotoxy(0, 0);
    
    stdout = &mystdout;
    
    TCCR1A = _BV(COM1A1) | _BV(COM1A0) | _BV(WGM10);
    TCCR1B = _BV(CS10);
    DDRD |= _BV(PD5);
}


/*************************************************************************
 * nastaveni kontrastu displeje
 *************************************************************************/ 
void LcdSetContrast(unsigned char contrast)
{
    if ((contrast < LCD_CONTRAST_MIN) || (contrast > LCD_CONTRAST_MAX))
        contrast = LCD_CONTRAST_MAX;
    
    OCR1A = (((unsigned char)(contrast << 2)) | 0x03);
}


/*************************************************************************
 * callback funkce pro zapis na zarizeni pomoci printf
 *************************************************************************/ 
static int lcd_putchar(char c, FILE *stream)
{
    lcd_putc(c);
    return 0;
}
